<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/16
 * Time: 14:04
 */

namespace App\Http\Classes;
use Illuminate\Support\Facades\DB;

class Common
{
    public static function getRand(){
        $length = 6;
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min,$max);
    }

    public static function response($status,$message,$data){
        $response = [];
        $response['status'] = $status;
        $response['message'] = $message;
        $response['data'] = $data;
        return response()->json($response);
    }

    public static function getSessionId(){
        $session_id = Common::getRand();
        $hash_session_id = hash('md5',$session_id);
        $temporary = DB::table('weibo_temporary');
        $result = $temporary->where('temporary_session',$hash_session_id)->count();
        if ($result){
            Common::getSessionId();
        }
        else{
            $temporary->insert(
                ['temporary_session' => $hash_session_id]
            );
            setcookie('php_session_id', $hash_session_id, time()+1800);
            return $hash_session_id;
        }
    }
}